package net.sx.minemodule.mvp.activities;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.UserService;
import com.homegoods.classify.model.Constant;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.crop.TakePhotoHelper;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineActivityMineUserInfoBinding;
import net.sx.minemodule.mvp.bean.UserBean;
import net.sx.minemodule.mvp.presenters.MineUserInfoActivityPresenter;
import net.sx.minemodule.mvp.views.MineUserInfoView;

import io.reactivex.Observable;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * 个人资料
 *
 * @author 11470
 */
@RuntimePermissions
@Route(path = ARouterPath.PATH_MINE_SETTING_USER_INFO)
public class MineUserInfoActivity extends MyBaseFragmentActivity<MineActivityMineUserInfoBinding, MineUserInfoActivityPresenter> implements MineUserInfoView {
    @Autowired(name = ARouterPath.PATH_USER_SERVICE)
    public UserService userService;

    private TakePhotoHelper photoHelper;

    @Override
    protected int getLayout() {
        return R.layout.mine_activity_mine_user_info;
    }

    @Override
    protected void initEvent() {
        mBinding.entryUserHeadUserInfo.setOnClickListener(v -> MineUserInfoActivityPermissionsDispatcher.takePhotoWithPermissionCheck(this));

        photoHelper.setOnTakePhotoCompleteListener(picStr -> Observable.fromIterable(picStr)
                .compose(bindToLifecycle())
                .take(1)
                .subscribe(imgUrl -> {
                    getPresenter().postKDSFile(userService.getSid(), imgUrl);
                }));

        //修改密码
        mBinding.entryLoginPasswordSafety.setOnClickListener(v -> {
            ARouter.getInstance().build(ARouterPath.PATH_MINE_UPDATE_LOGIN_PASSWORD).navigation();
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MineUserInfoActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        photoHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        photoHelper = new TakePhotoHelper(this);
        UserBean bean = new UserBean();
        bean.setPhone(userService.getMobile());
        bean.setUserHeadPath(Constant.SERVER_URL + userService.getHeadImg());
        bean.setUserName(userService.getNiName());
        mBinding.setItem(bean);
        mBinding.entryUserHeadUserInfo.setLeftTextAndRightImgRight("头像", Constant.SERVER_URL + userService.getHeadImg(), true);
        mBinding.entryUserNameUserInfo.setLeftTextAndRightTextRightImg("用戶名", userService.getNiName(), true);
//        mBinding.entryUserMemberUserInfo.setLeftTextAndRightTextRightImg("会员中心", "普通会员", true);
        mBinding.entryUserPhoneUserInfo.setLeftTextAndRightTextRightImg("绑定手机号", userService.getMobile(), true);
        mBinding.entryUserAddressUserInfo.setLeftTextAndRightTextRightImg("收货地址", "", true);
        mBinding.entryLoginPasswordSafety.setLeftTextAndRightImage("修改密码",true);

    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        ARouter.getInstance().inject(this);
        setTitle("个人资料");
    }

    @Override
    protected MineUserInfoActivityPresenter getPresenter() {
        return new MineUserInfoActivityPresenter(this);
    }

    @NeedsPermission({Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void takePhoto() {
        photoHelper.showPopup();
    }

    @Override
    public void showPhoto(String imgUrl, String userJson) {
        mBinding.entryUserHeadUserInfo.setRightImage(imgUrl);
        userService.setUserModel(userJson);
    }
}
